﻿namespace ClassyBlog.Infrastructure.Specs.Unit
{
    using System;
    using System.Collections.Generic;

    using Machine.Specifications;
    using NSubstitute;

    [Subject(typeof(DefaultDependencyResolver))]
    public class when_creating_default_dependency_resolver_with_null_container
    {
        static Exception exception;

        Because of = () => exception =
            Catch.Exception(() => new DefaultDependencyResolver(null));

        It should_throw_exception = () =>
            exception.ShouldBeOfType<ArgumentNullException>();
    }

    public abstract class DefaultDependencyResolverSpec
    {
        protected static IServiceContainer container;
        protected static DefaultDependencyResolver resolver;

        Establish context = () =>
        {
            container = Substitute.For<IServiceContainer>();
            resolver = new DefaultDependencyResolver(container);
        };
    }

    [Subject(typeof(DefaultDependencyResolver))]
    public class when_service_is_requested : DefaultDependencyResolverSpec
    {
        Because of = () => resolver.GetService(typeof(IFileSystem));

        It should_use_container = () =>
            container.Received().Resolve(typeof(IFileSystem));
    }

    [Subject(typeof(DefaultDependencyResolver))]
    public class when_services_are_requested : DefaultDependencyResolverSpec
    {
        static IEnumerable<object> services;

        Because of = () =>
            services = resolver.GetServices(typeof(IFileSystem));

        It should_be_empty = () => services.ShouldBeEmpty();
    }
}